/**
 *  combobox的空选择项设置。
 */
$.fn.combobox.defaults.emptyText="请选择";

$.fn.form.methods.clearValidateBox=function(jq) {
	jq.find('.easyui-validatebox').each(function(){
		$(this).removeClass("validatebox-invalid");
	});
	jq.find('.validatebox-invalid').each(function(){
		$(this).removeClass("validatebox-invalid");
	});
};

/**
 * 修改了form验证不了combobox的bug。
 */
$.fn.form.methods.validate=function(jq){
	var target =jq[0];
	if ($.fn.validatebox) {
		var box = $(".validatebox-text", target);
		if (box.length) {
			box.validatebox("validate");
			box.trigger("focus");
			box.trigger("blur");
			var invalidBox = $(".validatebox-invalid", target);
			invalidBox.each(function(){
				var $slef = $(this);
				if($slef.hasClass("easyui-combobox") || $slef.hasClass("combo-f") ){
					if($slef.combobox("validate")){
						$slef.removeClass("validatebox-invalid");
					};
				};
			});
			var valid = $(".validatebox-invalid:first", target).focus();
			return valid.length == 0;
		}
	}
	return true;
};

if($.fn.validatebox.defaults.rules){
	
	$.fn.validatebox.defaults.rules.notEmpty={
			validator : function(value) {
				return !/\s/.test(value);
			},
			message : " 该字段中包含空格！"
	};
	$.fn.validatebox.defaults.rules.telephone ={
		validator : function(value) {
			return /^([\+][0-9]{1,3}[ \.\-])?([\(]{1}[0-9]{2,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/.test(value);
		},
		message : '无效的联系电话'
	};
	$.fn.validatebox.defaults.rules.number={
			validator : function(value,param) {
				var len = $.trim(value).length;
				return len >= param[0] && len <= param[1]&&/^(0|[1-9][0-9]*)$/.test(parseInt(value));
			},
			message : '该字段必须为长度为{0}到{1}的数字！'
		};
	$.fn.validatebox.defaults.rules.length={
			validator : function(value,param) {
				var len = $.trim(value).length;
				return len === param[0];
			},
			message : '该字段长度必须为{0}！'
		};
	
	$.fn.validatebox.defaults.rules.max={
		validator : function(value, param) {
			var len = $.trim(value).length;
			return param[0]>=len;
		},
		message : '该字段的最大长度为 {0}'
	};
	$.fn.validatebox.defaults.rules.email={
		validator : function(value) {
			return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i
					.test(value);
		},
		message : 'Please enter a valid email address.'
	};
	
	$.extend($.fn.validatebox.defaults.rules, {    
	    monthValidate: {    
	    	validator: function (value, param) {
				 return (/^((0?[1-9])|((1|2)[0-9])|30|31)$/.test(value));   
	        },    
	        message: '请输入正确的天数'   
	    }    
	}); 
	$.extend($.fn.validatebox.defaults.rules, {    
	    hourValidate: {    
	    	validator: function (value, param) {
				 return (/^((\d(\.\d)?)|(1\d)(\.\d)?|(2[0-3])(\.\d)?|24(\.0)?)$/.test(value));   
	        },    
	        message: '请输入正确的小时数'   
	    }    
	}); 
	$.extend($.fn.validatebox.defaults.rules, {    
	    intValidate: {    
	    	validator: function (value, param) {
				 return (/0|^[0-9]*[1-9][0-9]*$/.test(value));   
	        },    
	        message: '请输入正确的金额'   
	    }    
	});
	$.fn.validatebox.defaults.rules.radio={
			validator : function(value) {
				var ipts = $('input[type="radio"][name="'+this.name+'"]');
				var lgth =ipts.length;
				for(var i=0,len=lgth;i<len;i++){
					var $thisDom = $.data(ipts[i], 'validate-item');
					if($thisDom&&$thisDom.tip){
						if($(ipts[i]).hasClass('sino-validatebox-invalid')){
							$(ipts[i]).removeClass('sino-validatebox-invalid');
						}
						$thisDom.tip.remove();
						$thisDom.tip=undefined;
					}
				}
				var length =$('input[type="radio"][name="'+this.name+'"]:checked').length;
				return length==0?false:true;
			},
			message : 'radio必填'
	};
	$.extend($.fn.validatebox.defaults.rules, {    
	    codeValidate: {    
	    	validator: function (value, param) {
				return (/^([0-9][0-9])$/.test(value));   
	        },    
	        message: '请输入两位数字编号,首位可使用0填充！'   
	    }    
	}); 
	$.extend($.fn.validatebox.defaults.rules, {    
		percentValidate: {    
	    	validator: function (value, param) {
				return (/^(0|[1-9]|([1-9][0-9])|100)$/.test(value));   
	        },    
	        message: '请输入请输入0-100的整数！'   
	    }    
	}); 
}

$(function(){
	
	$.extend($.fn.validatebox.defaults.rules, {    
	    hanzi: {    
	    	validator: function (value, param) {
				 return (/^[\u4e00-\u9fa5]{2,20}$/.test(value));   
	        },    
	        message: '只能为2-10的汉字'   
	    }    
	});  
	
	$.extend($.fn.validatebox.defaults.rules, {    
	    EorD: {    
	    	validator: function (value, param) {
				 return (/^\w+$/.test(value));   
	        },    
	        message: '只能为为英文或者数字和_'   
	    }    
	});  
	
	 $.extend($.fn.validatebox.defaults.rules, {    
			shuzi: {    
			    	validator: function (value, param) {  
			    		
						 return (/^[0-9]*[1-9][0-9]*$/gi.test(value));   
			        },    
			        message: '请输入正整数'   
			    }    
			});  
	 $.extend($.fn.validatebox.defaults.rules, {    
			zhekou: {    
			    	validator: function (value, param) {  
			    		
						 return (/^([1-9]{1,1}[0-9]{0,1}|0|100)$/.test(value));   
			        },    
			        message: '请输0-100整数'   
			    }    
			});  
	$.extend($.fn.validatebox.defaults.rules, {    
	   desc: {    
	    	validator: function (value, param) {
				 return (/^[^><]+$/ig.test(value));   
	        },    
	        message: '不能包含<号和>号'   
	    }    
	});  

	$.extend($.fn.validatebox.defaults.rules, {    
		urltest: {    
		    	validator: function (value, param) {  
		    		
					 return (/^[a-zA-Z]+:\/\/([a-zA-Z0-9_\-]+\.)?[a-zA-Z0-9_\-]+(:[0-9]+)?(\/?.*)?$/.test(value));   
		        },    		
		        message: '请输入合法的URL'   
		    }    
		});  
	
	$.extend($.fn.validatebox.defaults.rules, {    
		mobile: {    
		    	validator: function (value, param) {  
		    		
					 return (/^1\d{10}$/.test(value));   
					 
		        },    		
		        message: '请输入合法的手机号'   
		    }    
		});  
	
	
	$.extend($.fn.validatebox.defaults.rules, {    
		rightopt: {    
		    	validator: function (value, param) {  
		    		
					 return (/^(\/[0-9A-Za-z_\-]+)+.*?$/gi.test(value));   
		        },    
		        message: '请输入合法的操作'   
		    }    
		});  
	 $.extend($.fn.validatebox.defaults.rules, {
		 memail: {
			 validator: function (value, param) {
				 return /^[a-zA-Z0-9\._-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(value);
			 },
			 message: '邮箱格式不正确'
		 }
	 });
	$.extend($.fn.validatebox.defaults.rules, {
	    exist: {
	         validator: function(value, param) {
	             var postdata = {};
	             postdata[param[1]] = value;
	             var m_result =$.ajax({
	               type: "POST",//http请求方式
	               url: param[0],    //服务器段url地址
	               data:postdata,      //发送给服务器段的数据
	               dataType: "type", //告诉JQuery返回的数据格式
	               async: false
	            }).responseText;
	            if (m_result == "false") {
	                $.fn.validatebox.defaults.rules.exist.message = param[2];
	                return false;
	            }
	            else {
	            	
	               return true;
	            }
	            
	        },
	        message: ''
	    }
	});
		
});